package com.will.txj.wy.files.controller;

import com.will.txj.common.result.R;
import com.will.txj.common.result.RC;
import com.will.txj.wy.files.service.ISysFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;


/**
 * @author: wen-yi;
 * @date: 2021/12/3 22:45;
 * @Description: 文件操作
 */
@Slf4j
@RestController
@Api(value = "FDFS-API",tags = "FDFS控制层")
@RequestMapping("file/fast")
public class FDFSController {
    @Autowired
    private ISysFileService sysFileService;

    /**
     * 上传文件
     *
     * @param file 文件
     * @return 文件路径
     */
    @ApiOperation("上传普通文件")
    @ApiImplicitParam(name = "file",value = "需要上传的文件",required = true)
    @PostMapping("upload")
    public R uploadFile(MultipartFile file) {
        if (!file.isEmpty()){
            boolean flag = sysFileService.upFile(file);
            if (flag) return R.ok();
        }
        return R.fail(RC.FILE_ERROR);
    }

    /**
     * 上传图片文件
     *
     * @param file 文件
     * @return 文件路径
     */
    @ApiOperation("上传图片文件")
    @ApiImplicitParam(name = "file",value = "需要上传的图片",required = true)
    @PostMapping("image")
    public R uploadImageFile(MultipartFile file) {
        if (!file.isEmpty()){
            boolean flag = sysFileService.upImageFile(file);
            if (flag) return R.ok();
        }
        return R.fail(RC.FILE_ERROR);
    }

    @ApiOperation("下载文件")
    @ApiImplicitParam(name = "id",value = "文件ID",required = true)
    @PostMapping("download/{id}")
    public R downloadFile(@PathVariable("id") String id, HttpServletResponse response){
        if (StringUtils.isNotEmpty(id)){
            boolean flag = sysFileService.downloadFile(id, response);
            if (flag) {
                log.info("下载文件成功");
                return null;
            }
        }
        log.warn("下载文件失败 --> id:{}",id);
        return R.fail(RC.FILE_DOWN_FAIL);
    }

    @ApiOperation("删除文件")
    @ApiImplicitParam(name = "id",value = "文件ID",required = true)
    @PostMapping("delete/{id}")
    public R deleteFile(@PathVariable("id") String id){
        boolean flag = sysFileService.deleteFile(id);
        return flag?R.ok():R.fail(RC.FILE_NO_FOUND);
    }

    @ApiOperation("伪删除文件")
    @ApiImplicitParam(name = "id",value = "文件ID",required = true)
    @PostMapping("deleteById/{id}")
    public R deleteById(@PathVariable("id") String id){
        boolean flag = sysFileService.deleteFileById(id);
        return flag?R.ok():R.fail(RC.FILE_NO_FOUND);
    }

    @ApiOperation("预览文件")
    @ApiImplicitParam(name = "image",value = "文件签名",required = true)
    @GetMapping("show/{image}")
    public R show(@PathVariable("image") String image, HttpServletResponse response){
        try {
            boolean flag = sysFileService.showImage(image,response);
            return flag?null:R.fail(RC.FILE_NO_FOUND);
        } catch (Exception e){
            return R.fail(RC.PARAM_NOT_VALUE);
        }
    }
}
